package com.xunlai.infra.permission.manager.authority.config;

import com.xunlai.infra.permission.common.util.SM2Utils;
import com.xunlai.infra.permission.manager.authority.sercurity.passowrd.SM2PasswordEncoder;
import com.xunlai.infra.permission.manager.authority.sercurity.user.DelegateUserDetailsManager;
import com.xunlai.infra.permission.storage.repository.AuthGroupCrewRepository;
import com.xunlai.infra.permission.storage.repository.RoleRelationRepository;
import com.xunlai.infra.permission.storage.repository.UserAccountRepository;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author liang
 * @date 9/3/23 4:22 PM
 */
@Configuration
public class AuthorityConfig {


    @Bean
    public UserDetailsService users(UserAccountRepository userAccountRepository, RoleRelationRepository roleRelationRepository, AuthGroupCrewRepository authGroupCrewRepository) {
        return new DelegateUserDetailsManager(userAccountRepository, roleRelationRepository, authGroupCrewRepository);
    }
    @Bean
    public PasswordEncoder passwordEncoder(@Value("${user.password.encoder.privateKey}") String privateKey, @Value("${user.password.encoder.publicKey}") String publicKey){
        return new SM2PasswordEncoder(SM2Utils.loadPrivateKey(privateKey),SM2Utils.loadPublicKey(publicKey));
    }
}
